<template>
<Modal
	v-model="show"
	title="详细"
	:width="1000"
	>
	<div>
		<Form :model="info" :label-width="150" style="width: 100%" :rules="ruleValidate" ref="from2">
			<Row>
				<Col span="12">
					<FormItem label="月份：" >
						<Input :value="info.date_month" disabled placeholder="请输入关键词" class="defwid"></Input>
					</FormItem>
				</Col>
				<Col span="12">
					<FormItem label="地区：" prop="region">
						<region_list v-model="info.region" field="name" class="defwid"></region_list>
					</FormItem>
				</Col>
			</Row>
			<Row>
				<Col span="12">
					<FormItem label=" 日期：" prop="date">
						<DatePicker :value="info.date" type="date" placeholder="请选择日期" class="defwid" :options="options3" @on-change="date_change"></DatePicker>
					</FormItem>
				</Col>
				<Col span="12">
					<FormItem label=" 费用科目：" prop="subject">
						<subject_filter v-model="info.subject" field="name" class="defwid"></subject_filter>
					</FormItem>
				</Col>
			</Row>
			<Row>
				<Col span="12">
					<FormItem label="摘要：" prop="info">
						<Input type="textarea" v-model="info.info" placeholder="" :rows="4" class="defwid"></Input>
					</FormItem>
				</Col>
				<Col span="12">
					<FormItem label="承担部门：" prop="bumen">
						<bumen_info :value="info.bumen+'|'+info.bumen_scale" :isEdit="isEdit" field="name" @change="bumen_change"></bumen_info>
					</FormItem>
				</Col>
			</Row>
			<Row>
				<Col span="12">
					<FormItem label="币种：" prop="bz">
						<currency_list v-model="info.bz" field="name" class="defwid"></currency_list>
					</FormItem>
				</Col>
				<Col span="12">
					<FormItem label="凭证编号：" prop="certificate_m">
						<Input :value="info.certificate_m" disabled  class="defwid"></Input>
					</FormItem>
				</Col>
			</Row>
			<Row>
				<Col span="12">
					<FormItem label="收入/支出：" prop="mtype">
						<Select v-model="info.mtype"  placeholder="请选择" class="defwid">
							<Option value="0">收入</Option>
							<Option value="1">支出</Option>
						</Select>
					</FormItem>
				</Col>
				<Col span="12">
					<FormItem label="公司主体：" prop="company">
						<company_list v-model="info.company" field="sx_name" class="defwid"></company_list>
					</FormItem>
				</Col>
			</Row>
			<Row>
				<Col span="12">
					<FormItem label="金额：" prop="money">
						<Input v-model="info.money" placeholder="" class="defwid"></Input>
					</FormItem>
				</Col>
				<Col span="12">
					<FormItem label="操作人：" prop="username">
						<Input :value="info.username!=''?info.username:user.nickname" placeholder="" disabled  class="defwid"></Input>
					</FormItem>
				</Col>
			</Row>
		</Form>
	</div>
	<div slot="footer" style="    text-align: center;">
		<Button type="success" size="large"  @click="handleSubmit('from2')">提交</Button>
	</div>
</Modal>
</template>

<script>

import region_list from '@/themes/components/region_list';
import company_list from '@/themes/components/company_list';
import bumen_info from '@/themes/components/bumen_info';
import subject_filter from '@/themes/components/subject_filter';
import currency_list from '@/themes/components/currency_filter';


export default {
  name: 'info',
  components:{
		region_list,
		company_list,
		bumen_info,
		subject_filter,
		currency_list
	},
  data(){
	var self = this;
	return {
		options3: {
			disabledDate (date) {
				var curMonth = localStorage.account_balance;
				var ls_date = new Date(curMonth+'-01');
				var current = curMonth.split('-');
				if(parseInt(current[1]) === 12){
					var next_mo = '01';
					var next_ye = parseInt(current[0]) + 1;
				}else{
					var next_mo = (current[1]<9) ? '0'+parseInt(current[1])+1 : parseInt(current[1])+1;
					var next_ye = current[0];
				}
				var neMonth = next_ye+'-'+next_mo+'-01';
				var ne_date = new Date(neMonth);
				
				return date.valueOf() < ls_date.valueOf()-86400000 || date.valueOf() > ne_date.valueOf()-86400000 ;
			}
		},
		show:false,
		def_info:{
			date_month:'',
			date:'',
			certificate_m:'自动生成',
			info:'',
			money:'',
			region:'',
			bumen:'',
			bumen_scale:'',
			subject:'',
			company:'',
			bz:'人民币',
			mtype:0,
			username:'',
			type:0
		},
		info:{
			date_month:'',
			date:'',
			certificate_m:'自动生成',
			certificate:'',
			info:'',
			money:'',
			region:'',
			bumen:'',
			bumen_scale:'',
			subject:'',
			company:'',
			bz:'人民币',
			mtype:0,
			username:'',
			type:0
		},
		ruleValidate:{
			region:[
				{ required: true, message: '请选择地区', trigger: 'blur' }
			],
			date:[
				{ required: true, message: '请选择日期', trigger: 'blur' }
			],
			/*
			bumen:[
				{ required: true, message: '请选择部门', trigger: 'blur' }
			],
			*/
			info:[
				{ required: true, message: '请填写摘要', trigger: 'blur' }
			],
			subject:[
				{ required: true, message: '请选择科目', trigger: 'blur' }
			],
			bz:[
				{ required: true, message: '请选择币种', trigger: 'blur' }
			],
			certificate_m:[
				{ required: true, message: '请填写凭证', trigger: 'blur' }
			],
			mtype:[
				{ required: true, message: '请选择收入支出', trigger: 'blur' }
			],
			company:[
				{ required: true, message: '请选择公司主体', trigger: 'blur' }
			],
			money:[
				{ required: true, message: '请填写金额', trigger: 'blur' }
			],
			/*
			username:[
				{ required: true, message: '请填写操作人', trigger: 'blur' }
			],
			*/
		},
		companyList:[],
		id:0
	};
  },
  props:['value','cinfo','date_month','isEdit'],
  created(){
	
	
	this.show = this.value;
  },
  watch:{
	value(val){
		if(val){
			
			if(this.cinfo){
				for(var i in this.info){
					if(typeof(this.cinfo[i])!='undefined'){
						this.info[i] = this.cinfo[i];
					}
					else{
						this.info[i] = this.def_info[i];
					}
				}
				if(this.cinfo.id){
					this.id = this.cinfo.id;
				}
				else{
					this.id =0;
				}
			}
			this.info.date_month = this.date_month;
			this.info.date = this.date_month?this.date_month+'-01':'';
			if(this.info.bz == 'all'){
				this.info.bz = '人民币';
			}
		}
		this.show = val;
	},
	show(val){
		this.$emit('input',val);
	}
  },
  computed:{
	user(){
		return this.$store.state.user.user_info;
	}
  },
  methods: {
	handleSubmit(name) {
		this.$refs[name].validate((valid) => {
			if (valid) {
				this.einfo_ok();
			} 
		})
	},
	einfo_ok(){
	
		var self = this;
		var data =  [];
		var postdata = this.info;
		postdata['id'] = this.id;
		
		if(parseInt(postdata.money) > localStorage.setMaxMoney){
		
			if(confirm('当前输入金额过大，请再检查一遍噢~')){
				Whttp.load('Accounting/Cash/save',postdata,function(result){
					self.$Message.info('提交成功');
					self.$emit('success',result);
				});
			}
		}else{
			Whttp.load('Accounting/Cash/save',postdata,function(result){//console.log(result);return false;
				self.$Message.info('提交成功');
				self.$emit('success',result);
			});
		}
		
	},
	date_change(date){
		this.info.date = date;
	},
	bumen_change(val){
		var bumens = [];
		var bumen_scales = [];
		for(var i=0;i<val.length;i++){
			bumens.push(val[i].value);
			bumen_scales.push(val[i].scale);
		}
		
		this.info.bumen = bumens.join(',');
		this.info.bumen_scale = bumen_scales.join(',');
	}
  }
}
</script>
<style scoped>
.price{
	color: red;
    margin: 0 5px;
}
.defwid{
	width:220px;
}
</style>